#include "../util/TcpListener.h"
#include "Dsm.h"
#include <sstream>
#include <stdio.h>
#include <stdlib.h>


extern "C" {
        #include <sys/time.h>
}

void usage() {
        cout << "Usage: DsmTimeServer <no-parameters>" << endl;
        exit(1);
}


int main(int argc, char** argv) {
        if (argc > 2) {
                usage();
        }
        PracticaCaso::DsmDriver * driver = new PracticaCaso::DsmDriver("127.0.0.1", 1234, "dsm.deusto.es");
        PracticaCaso::DsmData data;
        cout << "Node created with NID: " << driver->get_nid() << endl;
          
		char buffer[100];
		char bufferTemp[4];
		char bufferHum[3];
		char bufferError[6] = "XXXXX";
		int humedad;
		int temperatura;
        struct timeval tv;
        time_t curtime;
       
    try
    {
        driver->dsm_malloc("ENVIRONMENT_TEMPERATURE", sizeof(int));
    } catch (DsmException excepcion)
    {
        cerr << "ERROR creating ENVIRONMENT_TEMPERATURE: " << excepcion << endl;
        exit(1);
    }
	try
    {
		driver->dsm_malloc("ENVIRONMENT_HUMIDITY", sizeof(int));
    } catch (DsmException excepcion)
    {
        cerr << "ERROR creating ENVIRONMENT_HUMIDITY: " << excepcion << endl;
        exit(1);
    }
	try {
        driver->dsm_malloc("ALERT", sizeof(bufferError));
    } catch (DsmException excepcion)
    {
        cerr << "ERROR creating ALERT: " << excepcion << endl;
        exit(1);
    }
	try {			
        driver->dsm_put("ALERT", (void *)bufferError, sizeof(bufferError));	
		} catch (DsmException excepcion) {
        cerr << "ERROR setting ALERT: " << excepcion << endl;
        driver->dsm_free("ALERT");
        exit(1);
		}
    while (1) {
		bool cond = false;
         while (!cond) {
            try {
                data = driver->dsm_get("ALERT");
                cond = true;
               } catch (DsmException excepcion) {
                cerr << "ERROR getting ALERT: " << excepcion  << endl;
                driver->dsm_wait("ALERT");
              }
		     }
		char* error;
		error = ((char *)data.addr);
		if (!strcmp(error,"error")) {
		cout << "CRITICAL ERROR in the acclimatization of the place!!!" << endl;
		cout << "System is turning OFF!" << endl;
		driver->dsm_free("ENVIRONMENT_TEMPERATURE");
		driver->dsm_free("ENVIRONMENT_HUMIDITY");
		driver->dsm_free("ALERT");
		exit(1);
		}
        gettimeofday(&tv, NULL);
        curtime=tv.tv_sec;
        strftime(buffer,100,"%d-%m-%Y, %H:%M:%S",localtime(&curtime));
        cout << "System date: " << buffer << endl;
		temperatura = rand() % 1001;
		humedad = rand() %101;
		sprintf(bufferHum, "%d", humedad);
		sprintf(bufferTemp,"%d",temperatura);
		cout << "System temperature: " << temperatura << endl;
		cout << "System humidity: " << humedad << "%" << endl;
		
        try {		
            driver->dsm_put("ENVIRONMENT_TEMPERATURE", (void *)bufferTemp, sizeof(bufferTemp));	
        } catch (DsmException excepcion) {
            cerr << "ERROR setting ENVIRONMENT_TEMPERATURE: temperatura -> " << excepcion << endl;
            driver->dsm_free("ENVIRONMENT_TEMPERATURE");
            exit(1);
        }
		try {			
            driver->dsm_put("ENVIRONMENT_HUMIDITY", (void *)bufferHum, sizeof(bufferHum));	
        } catch (DsmException excepcion) {
            cerr << "ERROR setting ENVIRONMENT_HUMIDITY: humedad ->: " << excepcion << endl;
            driver->dsm_free("ENVIRONMENT_HUMIDITY");
            exit(1);
        }		
        sleep(1);
        }
        driver->dsm_free("ENVIRONMENT_TEMPERATURE");
		driver->dsm_free("ENVIRONMENT_HUMIDITY");
		driver->dsm_free("ALERT");
}
